package com.frogs.ucenter.v1.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.system.web.api.response.FailResponse;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.AppBaseException;
import com.frogs.ucenter.v1.request.UserFamilyMemberAddRequest;
import com.frogs.ucenter.v1.request.UserFamilyMemberDeleteRequest;
import com.frogs.ucenter.v1.request.UserFamilyMemberListRequest;
import com.frogs.ucenter.v1.request.UserFamilyMemberShowRequest;
import com.frogs.ucenter.v1.request.UserFamilyMemberUpdateRequest;
import com.frogs.ucenter.v1.service.UserFamilyMemberService;
import com.frogs.ucenter.v1.util.ApiConstants;

/**
 * @author 用户家庭成员
 *
 */
@Controller(ApiConstants.VERSION+"userFamilyMemberController")
@RequestMapping("/"+ApiConstants.VERSION)
public class UserFamilyMemberController {
	
	@Resource(name=ApiConstants.VERSION+"userFamilyMemberService")
	private UserFamilyMemberService userFamilyMemberService;
	
	private static final String USER_FAMILY_MEMBER_ADD = "/user/family/add";
	private static final String USER_FAMILY_MEMBER_DELETE = "/user/family/delete";
	private static final String USER_FAMILY_MEMBER_UPDATE = "/user/family/update";
	private static final String USER_FAMILY_MEMBER_LIST = "/user/family/list";
	private static final String USER_FAMILY_MEMBER_DETAIL = "/user/family/detail";
	
	@RequestMapping(USER_FAMILY_MEMBER_ADD)
    public Response userFamilyMemberAdd(HttpServletRequest request, ModelMap model) {
    	Response response = null ; 
        try{
        	UserFamilyMemberAddRequest addRequest = new UserFamilyMemberAddRequest();
	    	addRequest.parse(request);
	    	response = userFamilyMemberService.insert(addRequest);
	    }catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
	    return response;
    }
	
	@RequestMapping(USER_FAMILY_MEMBER_DELETE)
	public Response userFamilyMemberDelete(HttpServletRequest request, ModelMap model) {
		Response response = null ; 
		try{
			UserFamilyMemberDeleteRequest deleteRequest = new UserFamilyMemberDeleteRequest();
			deleteRequest.parse(request);
			response = userFamilyMemberService.delete(deleteRequest);
		}catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	@RequestMapping(USER_FAMILY_MEMBER_UPDATE)
	public Response userFamilyMemberUpdate(HttpServletRequest request, ModelMap model) {
		Response response = null ; 
		try{
			UserFamilyMemberUpdateRequest updateRequest = new UserFamilyMemberUpdateRequest();
			updateRequest.parse(request);
			response = userFamilyMemberService.update(updateRequest);
		}catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	@RequestMapping(USER_FAMILY_MEMBER_LIST)
	public Response userFamilyMemberList(HttpServletRequest request, ModelMap model) {
		Response response = null ; 
		try{
			UserFamilyMemberListRequest listRequest = new UserFamilyMemberListRequest();
			listRequest.parse(request);
			response = userFamilyMemberService.getPageList(listRequest);
		}catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	@RequestMapping({USER_FAMILY_MEMBER_DETAIL})
	public Response userFamilyMemberDetail(HttpServletRequest request, ModelMap model) {
		Response response = null ; 
		try{
			UserFamilyMemberShowRequest infoRequest = new UserFamilyMemberShowRequest();
			infoRequest.parse(request);
			response = userFamilyMemberService.getObject(infoRequest);
		}catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
}
